<?php
class Activities_model extends CI_Model {

	public function __construct()
    {
		parent::__construct();
		$this->load->database();
    }
	
	/* Basic DB opeartion*/
    
	public function get_all()
    {
		$this->db->order_by("act_id","desc");
		$query = $this->db->get('activities');
		return $query->result_array();
    }
  
    public function get_by_id($act_id)
    {
		$query = $this->db->get_where('activities', array('act_id' => $act_id));
		return $query->row_array();
    }
  
    public function insert($data)
    {
		$this->db->insert('activities',$data);
		return $this->db->insert_id();
	}
	
    public function update($act_id,$data)
    {
		$this->db->update('activities', $data, array('act_id' => $act_id));
		return $this->db->affected_rows();
    }
	
	public function delete($act_id)
	{
		$this->db->delete('activities', array('act_id' => $act_id)); 
		return $this->db->affected_rows();
	}
	
	/*Business logic opeartion*/
	public function search($keyword,$page_no,$per_page)
	{
		$this->db->like('concat(act_title,act_date,act_movie)',$keyword, 'both');
		$query = $this->db->get('activities');
		$total = $query->num_rows();
		
		$this->db->like('concat(act_title,act_date,act_movie)',$keyword, 'both');
		$this->db->order_by('act_id', 'desc'); 
		$this->db->limit($per_page, $per_page*($page_no-1));
		$query = $this->db->get('activities');
		$result = $query->result_array();
		
		return array("result"=>$result,"total"=>$total);
	}
}
?>